package org.nokatag.elfunction;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.nokatag.password.AESPlus;
import org.nokatag.system.Concest;


/**
 * el方法
 * @author rebin
 *
 */
public class ELFunction {
	public static void main(String[] args){
		System.out.println(DecimalFormat(445.3,"##,##.00"));
	}
	/**
	 * 格式化
	 * @param d
	 * @param f
	 * @return
	 */
	public static  String DecimalFormat(Object d,String f){
		DecimalFormat df = new DecimalFormat(f);
		if(null!=d && null!=f){
			return df.format(d);
		}else if(null!=d){
			return d.toString();
		}
		return "";
	}
	/**
	 * 连接地址编码
	 * @param ur
	 * @param pag
	 */
	public static String ALink(String ur,javax.servlet.jsp.PageContext pag){
		HttpServletRequest request = (HttpServletRequest) pag.getRequest();
		HttpServletResponse response=(HttpServletResponse) pag.getResponse();
		try{
			String url = ur;
			if(!url.startsWith("http://")){
				if(url.startsWith("/"))
					url = rooturl(request)+url;
				else
					url = rooturl(request)+"/"+url;
			}
			pag.getOut().write(response.encodeURL(url));
		}catch(Exception se){
		}
		return "";
	}
	/**
	 * 解密字符串
	 * @param str
	 * @return
	 */
	public static String decryptStr(String str){
		return AESPlus.decrypt(str,Concest.SQLKEY);
	}
	/**
	 * 加密str
	 * @param str
	 * @return
	 */
	public static String encryptStr(String str){
		return AESPlus.encrypt(str,Concest.SQLKEY);
	}
	/**
	 * 字符串的split方法
	 * @param v
	 * @param p
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static List split(String v,String p){
		if(v!=null){
			if(p==null || p.trim().length()<1)
				p=",";
			List<String> ar = new ArrayList<String>();
			for(String s:v.split(p)){
				ar.add(s);
			}
			return ar;
		}
		return null;
	}
	/**
	 * 取得字符串的值
	 * @param v
	 * @param p
	 * @param i
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static String spvalue(String v,String p,Integer i){
		List list = split(v,p);
		if(list!=null){
			return list.get(i).toString();
		}
		return null;
	}
	/**
	 * 判断字符串是否为空或空字符
	 * @param v
	 * @return
	 */
	public static Boolean isEmpty(String v){
		return (v==null || v.trim().length()<1);
	}
	/**
	 * 年龄
	 * @param bro
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static Long age(String bro){
		try{
		Date dt1= new Date(bro.replace("-","/")); 
		Date dt2= new Date(); 
		long i= (dt2.getTime() - dt1.getTime())/(1000*60*60*24)/365+1; 
		return i;
		}catch(Exception e){
			return 0l;
		}
	}
	/**
	 * 格式化时间
	 * @param d
	 * @param f
	 * @return
	 */
	public static String formatdate(Date d,String f,Boolean isnow){
		if(f==null || f.trim().length()<1)
			f="yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat bartDateFormat = new SimpleDateFormat(f); 
		if(d==null){
			if(isnow)
				return bartDateFormat.format(new Date());
			else
				return "";
		}
		return bartDateFormat.format(d);
	}
	
	public static String rooturl(HttpServletRequest request){
		String path = request.getContextPath();
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
		if("1".equals(Concest.ROOT_PATH_RULE))
			return basePath;
		else if("2".equals(Concest.ROOT_PATH_RULE))
			return path;
		else if("3".equals(Concest.ROOT_PATH_RULE))
			return "";
		return basePath;
	}
}
